<?php


namespace app\admin\controller;


use app\admin\common\Lunar;
use app\admin\library\Auth;
use app\BaseController;
use think\facade\Session;
use think\facade\View;
use think\Model;
use workflow\workflow;

class Wf extends BaseController
{

    public static function work()
    {
        return new workflow();
    }


    public static function btn($wf_fid, $wf_type, $status)
    {
        $url = ["wf_type" => $wf_type, "wf_title" => '2', 'wf_fid' => $wf_fid];
        $url_star = url("/index/wf/wfstart/", ["wf_type" => $wf_type, "wf_title" => '2', 'wf_fid' => $wf_fid]);
        switch ($status) {
            case 0:
                return '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="faqi_approval"><i class="layui-icon layui-icon-read"></i>发起工作流</a>';
                break;
            case 1:
                $st = 0;
                $user_name = '';
                $flowinfo = self::work()->workflowInfo($wf_fid, $wf_type, ['uid' => Auth::instance()->id, 'role' => Auth::instance()->getGroups()[0]['id']]);
                if ($flowinfo != -1) {
                    if (!isset($flowinfo['status'])) {
                        return '<span class="btn btn-danger  radius size-S" onclick=javascript:alert("提示：当前流程故障，请联系管理员重置流程！")>Info:流程故障</span>';
                    }
                    if ($flowinfo['sing_st'] == 0) {
                        $user = explode(",", $flowinfo['status']['sponsor_ids']);
                        $user_name = $flowinfo['status']['sponsor_text'];
                        //3自由选择|4指定人员|5指定角色|6事务接受
                        if ($flowinfo['status']['auto_person'] == 3 || $flowinfo['status']['auto_person'] == 4 || $flowinfo['status']['auto_person'] == 6) {
                            if (in_array(Auth::instance()->id, $user)) {
                                $st = 1;
                            }
                        }
                        if ($flowinfo['status']['auto_person'] == 5) {
                            if (in_array(Auth::instance()->getGroups()[0]['id'], $user)) {
                                $st = 1;
                            }
                        }
                    } else {
                        if ($flowinfo['sing_info']['uid'] == Auth::instance()->id) {
                            $st = 1;
                        } else {
                            $admin = \app\admin\model\Admin::find($flowinfo['sing_info']['uid']);
                            $user_name = $admin->nickname ?? $flowinfo['sing_info']['uid'];
                        }
                    }
                } else {
                    return '<span class="btn  radius size-S">无权限</span>';
                }
                // <span class="btn  radius size-S" onclick=layer_show(\'审核\',"'.$url.'","850","650")>审核('.$user_name.')</span>
                if ($st == 1) {

                    return '<a class="layui-btn layui-btn-normal layui-btn-xs approval" data-type="'.$wf_type.'" data-id="'.$wf_fid.'">
<i class="layui-icon layui-icon-read"></i>审核(' . $user_name . ')</a>';
                } else {
                    return '<a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-read"></i>无权限(' . $user_name . ')</a>';
                }
//                if ($st == 1) {
//                    $data = [
//                        'url' => "flow/check",
//                        'title' => '审核',
//                        'class' => 'layui-btn-normal',
//                        'username' => $user_name,
//                        'step' => '当前',
//                        'lay' => 'approval'
//                    ];
//                    return $data;
//
//                } else {
//                    $data = [
//                        'title' => '无权限',
//                        'class' => 'layui-btn-disabled',
//                        'username' => $user_name,
//                        'step' => '步骤',
//                        'lay' => ''
//                    ];
//                    return $data;
//                }
            case 2:
                return '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-submit lay-filter="sh-check" data-type="'.$wf_type.'" data-id="'.$wf_fid.'"><i class="layui-icon layui-icon-read"></i>查看</a>';
            default:
                return '';
        }
    }


    //状态
    public static function status($status)
    {
        switch ($status) {
            case 0:
                return '<span class="label radius">保存中</span>';
                break;
            case 1:
                return '<span class="label radius" >流程中</span>';
                break;
            case 2:
                return '<span class="label label-success radius" >审核通过</span>';
                break;
            default: //-1
                return '<span class="label label-danger radius" >退回修改</span>';
        }
//        switch ($status) {
//
//            case 0:
//                return json(['code' => 200, 'msg' => '流程中', 'data' => ['class' => 'layui-btn-primary']]);
//                break;
//            case 1:
//                return json(['code' => 200, 'msg' => '流程中', 'data' => ['class' => 'layui-btn-normal']]);
//                break;
//            case 2:
//                return json(['code' => 200, 'msg' => '审核通过', 'data' => ['class' => 'layui-btn-warm']]);
//                break;
//            default: //-1
//                return json(['code' => 200, 'msg' => '退回', 'data' => ['class' => 'layui-btn-danger']]);
//        }
    }
}